import {
  myGet
} from '../../request/index'
import {
  baseRecUrl,
  getScoreRecords,
  getRecommendRecords
} from '../../request/api'

const app = getApp()

Page({
  data: {
    userInfo: {
      score: '0',
      amount: '0.00',
      name: '',
      avatar: '',
      cityName: '',
      provinceName: ''
    },
    active: 0,
    baseRecUrl: baseRecUrl,
    customBar: app.globalData.statusBarHeight + 44,
    recommendRecords: [],
    recommendPageInfo: {
      page: 1,
      limit: 10,
      total: 0,
      status: 2,
      refresh: false
    },
    scoreRecords: [],
    scorePageInfo: {
      page: 1,
      limit: 10,
      total: 0,
      status: 2,
      refresh: false
    },
  },
  onLoad() {
    this.refreshRecommendRecords()
    this.refreshScoreRecords()
  },
  toPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.path,
    })
  },
  tabChange(e) {
    this.setData({
      active: e.detail.index
    })
  },
  setCurrentUserInfo(e) {
    this.setData({
      userInfo: e.detail
    })
  },
  chooseavatar(e) {
    this.setData({
      'userInfo.avatarUrl': e.detail.avatarUrl
    })
    wx.setStorageSync('userInfo', this.data.userInfo)
  },
  onNickNameInputChange(e) {
    console.log(e);
    this.setData({
      'userInfo.nickName': e.detail.value
    })
    wx.setStorageSync('userInfo', this.data.userInfo)
  },
  nicknamereview(e) {
    console.log('昵称', e);
    this.setData({
      'userInfo.avatarUrl': e.detail.avatarUrl
    })
  },
  phoneCancel() {
    wx.showTabBar()
    this.setData({
      showGetPhone: false
    })
  },
  refreshRecommendRecords() {
    this.getRecommendRecords(true)
  },
  getRecommendRecords(refresh) {
    if (refresh && typeof refresh === 'boolean') {
      this.setData({
        recommendPageInfo: {
          page: 1,
          limit: 10,
          status: 1,
          refresh: true
        }
      })
    } else if (this.data.recommendPageInfo.status === 0) {
      return
    } else {
      this.setData({
        recommendPageInfo: {
          page: this.data.recommendPageInfo.page + 1,
          limit: 10,
          status: 2
        }
      })
    }
    myGet(getRecommendRecords, this.data.recommendPageInfo, (res) => {
      if (res.status === 200) {
        const list = res.data.records
        if (refresh && typeof refresh === 'boolean') {
          this.setData({
            recommendRecords: list,
            "recommendPageInfo.status": 1,
            "recommendPageInfo.refresh": false
          })
          return
        }
        this.data.recommendRecords.push(...list)
        this.setData({
          recommendRecords: this.data.recommendRecords,
          'recommendPageInfo.status': this.data.recommendPageInfo.page >= res.data.pages ? 0 : 1
        })
      }
    })
  },
  refreshScoreRecords() {
    this.getScoreRecords(true)
  },
  getScoreRecords(refresh) {
    if (refresh && typeof refresh === 'boolean') {
      this.setData({
        scorePageInfo: {
          page: 1,
          limit: 10,
          status: 1,
          refresh: true
        }
      })
    } else if (this.data.scorePageInfo.status === 0) {
      return
    } else {
      this.setData({
        scorePageInfo: {
          page: this.data.scorePageInfo.page + 1,
          limit: 10,
          status: 2
        }
      })
    }
    myGet(getScoreRecords, this.data.scorePageInfo, (res) => {
      if (res.status === 200) {
        const list = res.data.records
        if (refresh && typeof refresh === 'boolean') {
          this.setData({
            scoreRecords: list,
            "scorePageInfo.status": 1,
            "scorePageInfo.refresh": false
          })
          return
        }
        this.data.scoreRecords.push(...list)
        this.setData({
          scoreRecords: this.data.scoreRecords,
          'scorePageInfo.status': this.data.scorePageInfo.page >= res.data.pages ? 0 : 1
        })
      }
    })
  },
  toIndex() {
    wx.showTabBar()
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onPageScroll(e) {
    const navigationBar = this.selectComponent("#navigationBar")
    navigationBar.onScroll(e.scrollTop)
  },
})